package com.kgc.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.kgc.mapper.UserMapper;
import com.kgc.pojo.User;

@Service("userService")
public class UserServiceImpl implements UserService
{
    @Resource
    private UserMapper userMapper;
    
    public User selectUserByCodeAndPassword(User user)
        throws Exception
    {
        
        User u = userMapper.selectUser(user);
        if (u == null)
        {
            throw new Exception("用户不存在，请检查此用户");
        }
        return u;
    }
    
    @Override
    public List<User> selectAllUser()
    {
        return userMapper.selectAllUser();
    }
    
    @Override
    public Page selectAllUser(int pageNum)
    {
        Page page = PageHelper.startPage(pageNum, 10);
        
        userMapper.selectAllUser();
        return page;
    }
    
    @Override
    public User selectUserById(int id)
        throws Exception
    {
        
        return userMapper.selectUserById(id);
    }
    
    @Override
    public Page selectAllUser(int pageNum, String userName, Integer userRoleId)
    {
        Page page = PageHelper.startPage(pageNum, 10);
        
        userMapper.selectAllUserForNameAndRole(userName, userRoleId);
        return page;
    }
    
    @Override
    public List<User> selectUserByCode(String userCode)
    {
        
        return userMapper.selectUserByCode(userCode);
    }
    
    @Override
    public String deleteUserById(Integer uid)
    {
        User user = userMapper.selectUserById(uid);
        if (user == null)
        {
            return "notexist";
        }
        int count = userMapper.deleteUserById(uid);
        
        return count > 0 ? "true" : "false";
    }
    
}
